package com.lucky.ebidding.controller;

import com.lucky.ebidding.dto.*;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:AdminController
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/5/15 17:03
 * @Author:857251389@qq.com
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ExpertService expertService;

    @Autowired
    private PublishService publishService;

    @Autowired
    private ZbService zbService;

    @Autowired
    private TbService tbService;

    @Autowired
    private UserService userService;

    @GetMapping("/bam")
    public String bamJump(HttpServletRequest request, Model model) {

        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("user",user);
        return "admin/admin_bam";
    }

    @GetMapping("/bam1")
    public String bam1Jump(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("user",user);
        return "adminSh/admin1_bam";
    }

    @GetMapping("/expertCheckJump")
    public String expertCheckJump() {

        return "adminSh/expert_check";
    }

    @GetMapping("/publishSh")
    public String publishSh() {
        return "admin/publish_sh";
    }

    /**
     * 审核跳转
     *
     * @return
     */
    @GetMapping("/tbShowSh")
    public String tbShowSh() {

        return "admin/showSh";
    }

    /**
     * 审核跳转
     *
     * @return
     */
    @GetMapping("/zbShowSh")
    public String zbShowSh() {

        return "admin/zbSh";
    }




    @ResponseBody
    @GetMapping("/publishData")
    public PublishDTO publishData() {

        List<PublishInfo> publishInfos = publishService.selectAll();
        List<PublishAndZbAndTb> publishAndZbAndTbs = new ArrayList<>();
        for (PublishInfo publishInfo : publishInfos) {
            PublishAndZbAndTb publishAndZbAndTb = new PublishAndZbAndTb();
            ZbInfo zbInfo = zbService.selectById(publishInfo.getZbId());
            TbInfo tbInfo = tbService.selectById(publishInfo.getTbId());
            publishAndZbAndTb.setPublishInfo(publishInfo);
            publishAndZbAndTb.setZbInfo(zbInfo);
            publishAndZbAndTb.setTbInfo(tbInfo);
            publishAndZbAndTbs.add(publishAndZbAndTb);
        }

        PublishDTO publishDTO = new PublishDTO();
        publishDTO.setCode(0);
        publishDTO.setMsg("yes");
        publishDTO.setCount(publishAndZbAndTbs.size());
        publishDTO.setData(publishAndZbAndTbs);

        return publishDTO;
    }

    @ResponseBody
    @GetMapping("/expertCheck")
    public ExpertDTO expertCheck() {

        List<Expert> experts = expertService.selectAllExpert();

        ExpertDTO expertDTO = new ExpertDTO();
        expertDTO.setCode(0);
        expertDTO.setMsg("yes");
        expertDTO.setCount(experts.size());
        expertDTO.setData(experts);
        return expertDTO;
    }

//    @GetMapping("/expertShDetail/{id}")
//    public String shDetail(@PathVariable(name = "id") Integer id,
//                           Model model) {
//        model.addAttribute("expertId", id);
//        return "admin/expert_sh_detail";
//    }

    @ResponseBody
    @PostMapping("/setExpertStatus")
    public ResultDTO setStatus(Integer status, Integer id,HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");

        int i = expertService.setStatus(id, status,user);

        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }

    @ResponseBody
    @PostMapping("/setStatus")
    public ResultDTO setPublishStatus(Integer status, Integer publishId
            , Integer tbUserId, Integer zbId,HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        int i = publishService.setStatus(status,publishId,zbId,tbUserId,user);

        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }


    @ResponseBody
    @PostMapping("/setZbStatus")
    public ResultDTO setZbStatus(Integer status, Integer id,HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        int i = userService.setStatus(status,id,user);

        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }

        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }


    @ResponseBody
    @PostMapping("/setTbStatus")
    public ResultDTO setTbStatus(Integer status, Integer id,HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        int i = userService.setStatus(status, id,user);
        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }



    @ResponseBody
    @PostMapping("/setZbInfoStatus")
    public ResultDTO setZbInfoStatus(Integer status, Integer zbId) {

        int i = zbService.setStatus(status,zbId);

        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }

        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }


    @ResponseBody
    @GetMapping("/getSh")
    public TbDTO getSh() {

        List<TbInfo> tbInfos = tbService.selectByBedFlag(0);
        ArrayList<TbAndZbIdDTO> tbAndZbIdDTOS = new ArrayList<>();
        for (TbInfo tbInfo : tbInfos) {
            ZbInfo zbInfo = zbService.selectById(tbInfo.getZbId());
            TbAndZbIdDTO tbAndZbIdDTO = new TbAndZbIdDTO();
            tbAndZbIdDTO.setTbInfo(tbInfo);
            tbAndZbIdDTO.setZbInfo(zbInfo);
            tbAndZbIdDTOS.add(tbAndZbIdDTO);
        }
        TbDTO tbDTO = new TbDTO();

        tbDTO.setData(tbAndZbIdDTOS);
        tbDTO.setCode(0);
        tbDTO.setMsg("yes");
        tbDTO.setCount(tbInfos.size());
        return tbDTO;
    }

    @ResponseBody
    @GetMapping("/getZbSh")
    public ZbDTO getZbSh() {

        List<ZbInfo> zbInfos = zbService.selectByKbFlag(0);
        ZbDTO zbDTO = new ZbDTO();

        zbDTO.setData(zbInfos);
        zbDTO.setCode(0);
        zbDTO.setMsg("yes");
        zbDTO.setCount(zbInfos.size());
        return zbDTO;
    }


    @GetMapping("/zbShJump")
    public String zbShJump(){

        return "adminSh/zb_user_check";
    }

    @GetMapping("/tbShJump")
    public String tbShJump(){

        return "adminSh/tb_user_check";
    }


    @ResponseBody
    @GetMapping("/zbCheck")
    public UserDTO zbCheck(){
        List<UserAndUserSh> userAndUserShes = userService.selectAllZbUser(0);

        UserDTO userDTO = new UserDTO();

        userDTO.setCode(0);
        userDTO.setMsg("招标资格审核");
        userDTO.setData(userAndUserShes);
        userDTO.setCount(userAndUserShes.size());

        return userDTO;
    }

    @ResponseBody
    @GetMapping("/tbCheck")
    public UserDTO tbCheck(){
        List<UserAndUserSh> userAndUserShes = userService.selectAllZbUser(1);

        UserDTO userDTO = new UserDTO();

        userDTO.setCode(0);
        userDTO.setMsg("招标资格审核");
        userDTO.setData(userAndUserShes);
        userDTO.setCount(userAndUserShes.size());

        return userDTO;
    }

    @GetMapping("/userJump")
    public String userJump(){
        return "adminSh/all_user";
    }

    @GetMapping("/allUser")
    @ResponseBody
    public AllUserDTO allUSer(){

        List<User> users = userService.selectAllUser();
        AllUserDTO allUserDTO = new AllUserDTO();
        allUserDTO.setCode(0);
        allUserDTO.setMsg("所有用户");
        allUserDTO.setData(users);
        allUserDTO.setCount(users.size());
        return allUserDTO;
    }

    @PostMapping("/tbZiGeSh")
    @ResponseBody
    public ResultDTO tbZiGeSh(Integer status,Integer tbId,Integer zbId,
                              HttpServletRequest request){

        User user = (User) request.getSession().getAttribute("user");


        int i = tbService.setStatus(tbId, status, zbId, user);
        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }

    @GetMapping("/zbModifySh")
    public String zbModifySh(){

        return "admin/zbModifySh";
    }

    @ResponseBody
    @PostMapping("/setZbModifyStatus")
    public ResultDTO setZbModifyStatus(Integer status,Integer zbUserId,Integer zbId,Integer modifyId,
                                       HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");

        int i = zbService.setZbModifyStatus(status,zbUserId,zbId,modifyId,user);

        ResultDTO resultDTO = new ResultDTO();
        if (i != 0) {
            resultDTO.setCode(200);
            resultDTO.setMsg("Yes");
            return resultDTO;
        }
        resultDTO.setCode(500);
        resultDTO.setMsg("No");
        return resultDTO;
    }
}
